package com.cet.qz.unit01.chapter1_3;

/**
 * @program: algorithm
 * @description: 返回链表中键最大的节点的值
 * @author: 陈恩涛
 * @create: 2021-04-16 21:12
 **/
public class Qz1_3_27 {

    private static class Node {
        private int item;
        private Node next;
    }

    private static int max(Node first) {
        if (first == null) {
            return 0;
        }
        int max = first.item;
        while (first.next != null) {
            int item = first.next.item;
            if (item > max) {
                max = item;
            }
            first = first.next;
        }
        return max;
    }

    //递归实现
    private static int max2(Node first) {
        if (first == null) {
            return 0;
        }
        if (first.item > max2(first.next)) {
            return first.item;
        } else {
            return  max2(first.next);
        }
    }

    public static void main(String[] args) {
        Node node1 = new Node();
        node1.item = 1;
        Node node2 = new Node();
        node2.item = 2;
        Node node3 = new Node();
        node3.item = 3;
        Node node4 = new Node();
        node4.item = 4;
        Node node5 = new Node();
        node5.item = 5;

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        System.out.println(max(node1));
        System.out.println(max2(node1));
    }
}
